package com.abbas.smartCity.ui;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;

import com.abbas.smartCity.R;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.ResponseBean;
import com.abbas.smartCity.retroft.ICallback;
import com.google.android.material.textfield.TextInputLayout;

import java.util.Map;
import java.util.Objects;

import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * 意见反馈页面
 */
public class AdviseActivity extends BaseActivity {

    private EditText titleEt;
    private TextInputLayout contentTil;
    private Button postBt;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_advise;
    }

    @Override
    protected String setCustomTitle() {
        return "意见反馈";
    }

    @Override
    protected void initView() {
        titleEt = (EditText) findViewById(R.id.title_et);
        contentTil = (TextInputLayout) findViewById(R.id.content_til);
        postBt = (Button) findViewById(R.id.post_bt);
    }

    @Override
    protected void initData() {
        Objects.requireNonNull(contentTil.getEditText()).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //当文本改变之后，如果长度大于150，删除150之后的数据。由于回调将在很短的时间之内完成，所以用户根本看不出是输入一个删一个，只能感受到无法继续输入
                if (editable.length()>150) {
                    editable.delete(150,editable.length());
                }
            }
        });
    }

    @Override
    protected void initEvent() {
        postBt.setOnClickListener((v) -> {
            final Map<String, Object> params = getMap();
            params.put("title",titleEt.getText().toString());
            params.put("content", Objects.requireNonNull(contentTil.getEditText()).getText().toString());
            final RequestBody requestBody = createRequestBody(params);
            api.postAdvise(requestBody).enqueue(new ICallback<ResponseBean>() {
                @Override
                public void onSuccess(Call<ResponseBean> call, ResponseBean data) {
                    showToast(data.getMsg());
                }
            });
        });
    }
}